package com.fjnldx.mes.service.scheduling.alarm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 告警自动任务Mapper
 */
@Mapper
@Component
public interface AlarmSchedulingMapper extends BaseMapper {

    /**
     * 映射关系-表名
     *
     * @return 映射表
     */
    @Select("SELECT mapping_table\n" +
            "FROM system_mapping_equipment\n" +
            "WHERE dic_bh = (\n" +
            "    SELECT dic_bh\n" +
            "    FROM fzy_dictionary_data_list\n" +
            "    WHERE dic_bh in\n" +
            "          (\n" +
            "              SELECT dic_bh\n" +
            "              FROM system_mapping_equipment\n" +
            "              WHERE gongxu = #{gongXu}\n" +
            "          )\n" +
            "      AND key_value = #{devSn}\n" +
            ")")
    String mappingRelationTable(String gongXu, String devSn);

    /**
     * 查询工序所有设备
     *
     * @return 映射表
     */
    @Select("SELECT key_value\n" +
            "FROM fzy_dictionary_data_list\n" +
            "WHERE dic_bh in (\n" +
            "    SELECT dic_bh\n" +
            "    FROM system_mapping_equipment\n" +
            "    WHERE gongxu = #{gongXu}\n" +
            ")")
    List<String> mappingDevSn(String gongXu);

    /**
     * 获取实时属性值
     *
     * @return 实时属性值
     */
    @Select("<script>" +
            "SELECT ${cs} FROM ${tableName}\n" +
            "WHERE 1=1\n" +
            "<if test='devSn!=null and devSn != \"\"'> AND devSn = #{devSn} </if>\n" +
            "<if test='conditionSql!=null and conditionSql !=\"\"'> AND ${conditionSql} </if>\n" +
            "ORDER BY time DESC LIMIT 1" +
            "</script>")
    String getCsFirstValue(String cs, String tableName, String devSn, String conditionSql);

    /**
     * 获取延迟属性值
     *
     * @param cs        属性字段
     * @param tableName 实时表
     * @param devSn     设备编号
     * @param time      延迟最大时间
     * @return 延迟属性值
     */
    @Select("<script>" +
            "SELECT ${cs} FROM ${tableName}\n" +
            "WHERE 1=1 " +
            "<![CDATA[  AND DATEDIFF(time,#{time})<=0  ]]>\n" +
            "<if test='devSn!=null and devSn != \"\"'> AND devSn = #{devSn} </if>\n" +
            "<if test='conditionSql!=null and conditionSql !=\"\"'> AND ${conditionSql} </if>\n" +
            "ORDER BY time DESC LIMIT 1" +
            "</script>")
    String getCsDelayValue(String cs, String tableName, String devSn, LocalDateTime time, String conditionSql);

    /**
     * 获取累计属性值
     *
     * @param cs        属性字段
     * @param tableName 实时表
     * @param devSn     设备编号
     * @param number    次数
     * @return 累次属性值
     */
    @Select("<script>" +
            "SELECT ${cs} FROM ${tableName}\n" +
            "WHERE 1=1\n" +
            "<if test='devSn!=null and devSn != \"\"'> AND devSn = #{devSn} </if>\n" +
            "<if test='conditionSql!=null and conditionSql !=\"\"'> AND ${conditionSql} </if>\n" +
            "ORDER BY time DESC LIMIT #{number}" +
            "</script>")
    List<String> getCsCumulativeValue(String cs, String tableName, String devSn, Integer number, String conditionSql);
}
